/*
 * Color.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.graphics;

/**
 * Color Class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class Color extends Paint {

// Public Static Fields. -------------------------------------------------------

  /**
   * The white color.
   */
  public static final Color WHITE = new Color(255, 255, 255);

  /**
   * The light gray color.
   */
  public static final Color LIGHT_GRAY = new Color(192, 192, 192);

  /**
   * The gray color.
   */
  public static final Color GRAY = new Color(128, 128, 128);

  /**
   * The dark gray color.
   */
  public static final Color DARK_GRAY = new Color(64, 64, 64);

  /**
   * The black color.
   */
  public static final Color BLACK = new Color(0, 0, 0);

  /**
   * The red color.
   */
  public static final Color RED = new Color(255, 0, 0);

  /**
   * The dark red color.
   */
  public static final Color DARK_RED = new Color(128, 0, 0);

  /**
   * The pink color.
   */
  public static final Color PINK = new Color(255, 175, 175);

  /**
   * The orange color.
   */
  public static final Color ORANGE = new Color(255, 200, 0);

  /**
   * The yellow color.
   */
  public static final Color YELLOW = new Color(255, 255, 0);

  /**
   * The dark yellow color.
   */
  public static final Color DARK_YELLOW = new Color(128, 128, 0);

  /**
   * The green color.
   */
  public static final Color GREEN = new Color(0, 255, 0);

  /**
   * The dark green color.
   */
  public static final Color DARK_GREEN = new Color(0, 128, 0);

  /**
   * The magenta color.
   */
  public static final Color MAGENTA = new Color(255, 0, 255);

  /**
   * The dark magenta color.
   */
  public static final Color DARK_MAGENTA = new Color(128, 0, 128);

  /**
   * The cyan color.
   */
  public static final Color CYAN = new Color(0, 255, 255);

  /**
   * The dark cyan color.
   */
  public static final Color DARK_CYAN = new Color(0, 128, 128);

  /**
   * The blue color.
   */
  public static final Color BLUE = new Color(0, 0, 255);

  /**
   * The dark blue color.
   */
  public static final Color DARK_BLUE = new Color(0, 0, 128);

// Private Fields. -------------------------------------------------------------

  private final int argb;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new Color object.
   *
   * @param argb the argb value of the color
   */
  public Color(int argb) {
    this.argb = argb;
  }

  /**
   * Constructs new Color object.
   *
   * @param r the red component of the color
   * @param g the green component of the color
   * @param b the blue component of the color
   */
  public Color(int r, int g, int b) {
    this(r, g, b, 255);
  }

  /**
   * Constructs new Color object.
   *
   * @param r the red component of the color
   * @param g the green component of the color
   * @param b the blue component of the color
   * @param a the alpha component of the color
   */
  public Color(int r, int g, int b, int a) {
    if ((a < 0) || (a > 255)) {
      throw new IllegalArgumentException(
                    "Invalid alpha component of the color");
    }
    if ((r < 0) || (r > 255)) {
      throw new IllegalArgumentException(
                    "Invalid red component of the color");
    }
    if ((g < 0) || (g > 255)) {
      throw new IllegalArgumentException(
                    "Invalid green component of the color");
    }
    if ((b < 0) || (b > 255)) {
      throw new IllegalArgumentException(
                    "Invalid blue component of the color");
    }
    argb = (((0xFF & a) << 24) | ((0xFF & r) << 16) | ((0xFF & g) << 8) |
            (0xFF & b));
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns the alpha component of the color.
   *
   * @return the alpha component of the color
   */
  public int getAlpha() {
    return (0xFF & (argb >> 24));
  }

  /**
   * Returns the red component of the color.
   *
   * @return the red component of the color
   */
  public int getRed() {
    return (0xFF & (argb >> 16));
  }

  /**
   * Returns the green component of the color.
   *
   * @return the green component of the color
   */
  public int getGreen() {
    return (0xFF & (argb >> 8));
  }

  /**
   * Returns the blue component of the color.
   *
   * @return the blue component of the color
   */
  public int getBlue() {
    return (0xFF & argb);
  }

  /**
   * Returns the RGB value representing the color.
   *
   * @return the RGB value representing the color
   */
  public int getRGB() {
    return 0xFF000000 | argb;
  }

  /**
   * Returns the ARGB value representing the color.
   *
   * @return the ARGB value representing the color
   */
  public int getARGB() {
    return argb;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if ((obj != null) && (obj.getClass() == getClass())) {
      Color color = (Color)obj;
      return (color.getARGB() == getARGB());
    }
    return false;
  }

  /**
   * {@inheritDoc}
   */
  public int hashCode() {
    return argb;
  }

  /**
   * {@inheritDoc}
   */
  public String toString() {
    return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() +
           ",b=" + getBlue() + ",a=" + getAlpha() + "]";
  }

}
